package com.td.module.member.controller.admin.appMemberUser;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.member.controller.admin.appMemberUser.vo.*;
import com.td.module.member.convert.appMemberUser.AppMemberUserConvert;
import com.td.module.member.dal.dataobject.appMemberUser.AppMemberUserDO;
import com.td.module.member.service.appMemberUser.AppMemberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - app用户")
@RestController
@RequestMapping("/member/app-member-user")
@Validated
public class AppMemberUserController {

    @Resource
    private AppMemberUserService appMemberUserService;

    @PostMapping("/create")
    @ApiOperation("创建app用户")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:create')")
    public CommonResult<Long> createAppMemberUser(@Valid @RequestBody AppMemberUserCreateReqVO createReqVO) {
        return success(appMemberUserService.createAppMemberUser(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新app用户")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:update')")
    public CommonResult<Boolean> updateAppMemberUser(@Valid @RequestBody AppMemberUserUpdateReqVO updateReqVO) {
        appMemberUserService.updateAppMemberUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除app用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:app-member-user:delete')")
    public CommonResult<Boolean> deleteAppMemberUser(@RequestParam("id") Long id) {
        appMemberUserService.deleteAppMemberUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得app用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:app-member-user:query')")
    public CommonResult<AppMemberUserRespVO> getAppMemberUser(@RequestParam("id") Long id) {
        AppMemberUserDO appMemberUser = appMemberUserService.getAppMemberUser(id);
        return success(AppMemberUserConvert.INSTANCE.convert(appMemberUser));
    }

    @GetMapping("/list")
    @ApiOperation("获得app用户列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('member:app-member-user:query')")
    public CommonResult<List<AppMemberUserRespVO>> getAppMemberUserList(@RequestParam("ids") Collection<Long> ids) {
        List<AppMemberUserDO> list = appMemberUserService.getAppMemberUserList(ids);
        return success(AppMemberUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得app用户分页")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:query')")
    public CommonResult<PageResult<AppMemberUserRespVO>> getAppMemberUserPage(@Valid AppMemberUserPageReqVO pageVO) {
        PageResult<AppMemberUserDO> pageResult = appMemberUserService.getAppMemberUserPage(pageVO);
        return success(AppMemberUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出app用户 Excel")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:export')")
    @OperateLog(type = EXPORT)
    public void exportAppMemberUserExcel(@Valid AppMemberUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AppMemberUserDO> list = appMemberUserService.getAppMemberUserList(exportReqVO);
        // 导出 Excel
        List<AppMemberUserExcelVO> datas = AppMemberUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "app用户.xls", "数据", AppMemberUserExcelVO.class, datas);
    }

    @GetMapping("/association")
    @ApiOperation("关联大户")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:update')")
    public CommonResult<Boolean> associationContract(@RequestParam("id") Long id){
        return success(appMemberUserService.associationContract(id));
    }

    @PutMapping("/update-password")
    @ApiOperation("重置用户密码")
    @PreAuthorize("@ss.hasPermission('member:app-member-user:update-password')")
    public CommonResult<Boolean> updateAppMemberUserPassword(@Valid @RequestBody AppMemberUserUpdatePasswordReqVO reqVO) {
        appMemberUserService.updateAppMemberUserPassword(reqVO.getId(), reqVO.getPassword());
        return success(true);
    }

    @PutMapping("/update-status")
    @ApiOperation("修改用户状态")
    public CommonResult<Boolean> updateAppMemberUserStatus(@Valid @RequestBody AppMemberUserUpdateStatusReqVO reqVO) {
        appMemberUserService.updateAppMemberUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

}
